package org.example.web;

import com.github.pagehelper.PageInfo;
import org.example.model.Admin;
import org.example.model.Menu;
import org.example.model.Result;
import org.example.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/adminApi/adminCtl")
public class AdminController {
    @Autowired
    AdminService adminService;

    @RequestMapping(path = "/admins",method = RequestMethod.POST)
    /*@ResponseBody 接收查询条件
    * */
    public Result admin(@RequestBody Admin admin) {
        PageInfo<Admin> adminLists = adminService.searchAllAdmins(admin);
        return new Result(200,"success", adminLists);
    }
    @RequestMapping(path = "/menus",method = RequestMethod.GET)
    /*查询所有type2的菜单
     * */
    public Result menus() {
        List<Menu> menus = adminService.searchAllMenus();
        return new Result(200,"success",menus);
    }
    @RequestMapping(path = "/addAdmin",method = RequestMethod.POST)
    /*添加管理员
     * */
    public Result addAdmin(@RequestBody Admin admin, @RequestHeader("adminToken")String adminToken) {
        if(admin.getId() == null){
            adminService.addAdmin(admin,adminToken);
        }else{
            adminService.updateAdmin(admin,adminToken);
        }

        return new Result(200,"success",null);
    }

    /**
     * 根据管理员id查询管理员信息
     */
    @RequestMapping(path = "/findAdminById",method = RequestMethod.GET)
    public Result findAdminById(Integer id) {
        Admin admin = adminService.findAdminById(id);
        return new Result(200,"success",admin);
    }
    /**
     * 根据管理员id删除管理员信息
     */
    @RequestMapping(path = "/deleteAdminById",method = RequestMethod.GET)
    public Result deleteAdminById(Integer id) {
        adminService.deleteAdminById(id);
        return new Result(200, "success", null);
    }




}
